package view.customer;

import com.jfoenix.controls.JFXCheckBox;
import dao.OrderDao;
import dao.UserDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;
import pojo.order.OrderEntity;
import pojo.order.OrderProperty;
import view.Main;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class CustomerOrderController {
    private Main myApp ;
    private static String Userid;
    private static String id;

    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    private static List<OrderEntity> orderEntities = new ArrayList<>() ;
    private static OrderDao orderDao = new OrderDao();
    private static UserDao userDao = new UserDao();

    public  void setId(String id) {
        this.id = id;
    }
    public String getId() throws IOException {
        return orderEntities.get(0).getId();
    }

    public String getUserid() {
        return Userid;
    }

    public void setUserid(String userid) {
        Userid = userid;
    }

    public CustomerOrderController(){}
    @FXML
    private TableView<OrderProperty> orderTable;

    @FXML
    private TableColumn<OrderProperty, Void> option;

    @FXML
    private TableColumn<OrderProperty, String> idCol;

    @FXML
    private TableColumn<OrderProperty, String> code;

    @FXML
    private TableColumn<OrderProperty, String> name;

    @FXML
    private TableColumn<OrderProperty, String> amount;

    @FXML
    private TableColumn<OrderProperty, String> handInDate;

    @FXML
    private TableColumn<OrderProperty, String> deadline;

    @FXML
    private TableColumn<OrderProperty, String> consignee;

    @FXML
    private TableColumn<OrderProperty, String> phone;

    @FXML
    private TableColumn<OrderProperty, String> address;

    @FXML
    private TableColumn<OrderProperty, String> stateForCustomer;



    @FXML
    void bidInfo(ActionEvent event) throws IOException {
        myApp.goToCustomerBidManage();
        this.showAllInfo();
    }

    @FXML
    void shouHuo(ActionEvent event) throws IOException {
        orderDao.shouHuo(orderEntities.get(0).getId());
        this.showAllInfo();
    }

    @FXML
    void changeOrder(ActionEvent event) throws Exception {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        if(orderEntities.size()>1) {
            alert.setHeaderText("!");
            alert.setContentText("只能选择一个哦");
            alert.showAndWait();
            orderEntities = new ArrayList<>();
            this.showAllInfo();
        }else if(orderEntities.size() == 0){
            alert.setHeaderText("!");
            alert.setContentText("至少选择一个");
            alert.showAndWait();
            orderEntities = new ArrayList<>();
            this.showAllInfo();
        }
        else{
            if(orderEntities.get(0).getOrderStateForCustomer().equals("已保存")){
                myApp.goToChangeOrderPage();
            }else{
                alert.setHeaderText("!");
                alert.setContentText("已经发布过啦，不能修改了哦");
                alert.showAndWait();
            }
            orderEntities = new ArrayList<>();
            this.showAllInfo();
        }
    }

    @FXML
    void deleteOrder(ActionEvent event) throws IOException {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        orderTable.setItems(FXCollections.emptyObservableList());
        for(OrderEntity orderEntity:orderEntities) {
            if (orderEntity.getOrderStateForCustomer().equals("已发布")) {
                alert.setHeaderText("!");
                alert.setContentText("订单已发布，不能删除");
                alert.showAndWait();
            } else  if (orderEntity.getOrderStateForCustomer().equals("已保存")){
                orderDao.deleteOrder(orderEntity.getId());
            }
        }
        this.showAllInfo();
}

    @FXML
    void newOrder(ActionEvent event) throws IOException {
        myApp.goToNewOrderPage();
        this.showAllInfo();
    }

    @FXML
    void putOrder(ActionEvent event) throws IOException {
        orderDao.changeCustomerState(orderEntities);
        this.showAllInfo();
    }

    public void showAllInfo() throws IOException {
        this.displayInfo(orderDao.findOrderByConsignee(userDao.findUserById(Userid).getName()));
    }

    private void addButtonToTable( TableColumn<OrderProperty, Void> column){
        Callback<TableColumn<OrderProperty, Void>, TableCell<OrderProperty, Void>> cellFactory = new Callback<TableColumn<OrderProperty, Void>, TableCell<OrderProperty, Void>>() {
            @Override
            public TableCell<OrderProperty, Void> call(final TableColumn<OrderProperty, Void> param) {
                final TableCell<OrderProperty, Void> cell = new TableCell<>() {

                    final JFXCheckBox btn = new JFXCheckBox();

                    {
                        btn.setOnAction((ActionEvent event) -> {
                            OrderProperty orderProperty = getTableView().getItems().get(getIndex());
                            try {
                                orderEntities.add(orderDao.findOrder(orderProperty.getId()));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        column.setCellFactory(cellFactory);

    }
    void displayInfo(List<OrderEntity> orderEntityList){
       orderTable.getItems();
        ObservableList<OrderProperty> orderProperties = FXCollections.observableArrayList();
        if(orderEntityList!=null){
            for(OrderEntity orderEntity:orderEntityList){
                OrderProperty orderProperty = new OrderProperty();
                orderProperty.setId(orderEntity.getId());
                orderProperty.setAddress(orderEntity.getAddress());
                orderProperty.setAmount(orderEntity.getAmount());
                orderProperty.setConsignee(orderEntity.getConsignee());
                orderProperty.setName(orderEntity.getName());
                orderProperty.setCode(orderEntity.getCode());
                orderProperty.setDeadLine(orderEntity.getDeahline());
                orderProperty.setOrderStateForCustomer(orderEntity.getOrderStateForCustomer());
                orderProperty.setHandInDate(orderEntity.getHandInDate());
                orderProperty.setPhone(orderEntity.getPhone());

                orderProperties.add(orderProperty);
            }
            addButtonToTable(option);

            idCol.setCellValueFactory(param -> param.getValue().idProperty());
            address.setCellValueFactory(param -> param.getValue().addressProperty());
            code.setCellValueFactory(param -> param.getValue().codeProperty());
            amount.setCellValueFactory(param -> param.getValue().amountProperty());
            deadline.setCellValueFactory(param -> param.getValue().deadLineProperty());
            consignee.setCellValueFactory(param -> param.getValue().consigneeProperty());
            name.setCellValueFactory(param -> param.getValue().nameProperty());
            phone.setCellValueFactory(param -> param.getValue().phoneProperty());
            handInDate.setCellValueFactory(param -> param.getValue().handInDateProperty());
            stateForCustomer.setCellValueFactory(param -> param.getValue().orderStateForCustomerProperty());
            orderTable.setItems(orderProperties);
        }else{
            orderTable.setItems(FXCollections.emptyObservableList());
        }
    }
}
